package com.codedancer.campus.client.api.dynamic;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.dynamic.ro.DynamicPageSearchRo;
import com.codedancer.campus.client.api.dynamic.ro.WhisperPublishRo;
import com.codedancer.campus.client.service.IDynamicService;
import com.codedancer.campus.client.service.bo.dynamic.DynamicPageSearchBo;
import com.codedancer.campus.client.service.bo.dynamic.WhisperPublishBo;
import com.codedancer.campus.client.service.vo.dynamic.DynamicPageVo;
import com.codedancer.campus.client.service.vo.dynamic.DynamicVo;
import com.codedancer.campus.common.anno.KudosNumber;
import com.codedancer.campus.common.anno.Limit;
import com.codedancer.campus.common.anno.ViewNumber;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.enums.KudosNumberKeyEnum;
import com.codedancer.campus.common.enums.ViewNumberKeyEnum;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "动态api", description = "动态api", tags = "动态api")
@RequestMapping("/api/dynamic")
public class DynamicApi extends BaseApi {

  @Autowired
  private IDynamicService dynamicService;

  @PostMapping("/publish-whisper")
  @ApiOperation("发布悄悄话")
  public ApiResponse<Void> publishWhisper(
      @RequestBody @Valid @ApiParam(value = "悄悄话发布Ro", required = true) WhisperPublishRo whisperPublishRo) {
    WhisperPublishBo whisperPublishBo = BeanUtil
        .copyProperties(whisperPublishRo, WhisperPublishBo.class);
    whisperPublishBo.setCurrentUuid(currentLoginUuid());
    dynamicService.publishWhisper(whisperPublishBo);
    return success();
  }

  @PutMapping("/update-whisper/{uuid}")
  @ApiOperation("修改悄悄话")
  public ApiResponse<Void> updateWhisper(
      @RequestBody @Valid @ApiParam(value = "悄悄话发布Ro", required = true) WhisperPublishRo whisperPublishRo,
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    WhisperPublishBo whisperPublishBo = BeanUtil
        .copyProperties(whisperPublishRo, WhisperPublishBo.class);
    whisperPublishBo.setUuid(uuid);
    dynamicService.updateWhisper(whisperPublishBo);
    return success();
  }

  @GetMapping("/page")
  @ApiOperation("分页查询动态")
  public ApiResponse<IPage<DynamicPageVo>> page(
      @ApiParam(value = "动态分页查询参数", required = false)
      DynamicPageSearchRo dynamicPageSearchRo) {
    DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
        .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
    dynamicPageSearchBo.setCurrentUserUuid(currentLoginUuid());
    return success(dynamicService.page(dynamicPageSearchBo));
  }

  @GetMapping("/detail/{uuid}")
  @ApiOperation("动态详情")
  @ViewNumber(description = "动态详情", key = ViewNumberKeyEnum.DYNAMIC)
  public ApiResponse<DynamicVo> detail(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    return success(dynamicService.detail(uuid));
  }

  @GetMapping("/page/user")
  @ApiOperation("查询用户发布的动态")
  public ApiResponse<IPage<DynamicPageVo>> page2User(
      @ApiParam(value = "动态分页查询参数", required = false)
      DynamicPageSearchRo dynamicPageSearchRo) {
    DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
        .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
    dynamicPageSearchBo.setCurrentUserUuid(currentLoginUuid());
    return success(dynamicService.page2User(dynamicPageSearchBo));
  }

  @DeleteMapping("/delete/{uuid}")
  @ApiOperation("删除动态")
  public ApiResponse<Void> delete(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.delete(uuid);
    return success();
  }

  @GetMapping("/page-by-circle-uuid/{circleUuid}")
  @ApiOperation("分页查询某个圈子下的动态")
  public ApiResponse<IPage<DynamicPageVo>> pageByCircleUuid(
      @ApiParam(value = "动态分页查询参数", required = false)
      DynamicPageSearchRo dynamicPageSearchRo,
      @PathVariable(value = "circleUuid", required = true) @ApiParam(value = "圈子uuid", required = true) String circleUuid) {
    DynamicPageSearchBo dynamicPageSearchBo = BeanUtil
        .copyProperties(dynamicPageSearchRo, DynamicPageSearchBo.class);
    dynamicPageSearchBo.setCircleUuid(circleUuid);
    dynamicPageSearchBo.setCurrentUserUuid(currentLoginUuid());
    return success(dynamicService.pageByCircleUuid(dynamicPageSearchBo));
  }

  @PutMapping("/open-comment/{uuid}")
  @ApiOperation("开启评论")
  public ApiResponse<Void> openComment(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.openComment(uuid);
    return success();
  }

  @PutMapping("/close-comment/{uuid}")
  @ApiOperation("关闭评论")
  public ApiResponse<Void> closeComment(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.closeComment(uuid);
    return success();
  }

  @PutMapping("/hide/{uuid}")
  @ApiOperation("隐藏")
  public ApiResponse<Void> hide(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.hide(uuid);
    return success();
  }

  @PutMapping("/display/{uuid}")
  @ApiOperation("显示")
  public ApiResponse<Void> display(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.display(uuid);
    return success();
  }

  @PutMapping("/sync-liberty-square/{uuid}")
  @ApiOperation("同步自由广场")
  public ApiResponse<Void> syncLibertySquare(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.syncLibertySquare(uuid);
    return success();
  }

  @PutMapping("/cancel-sync-liberty-square/{uuid}")
  @ApiOperation("取消同步自由广场")
  public ApiResponse<Void> cancelSyncLibertySquare(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.cancelSyncLibertySquare(uuid);
    return success();
  }

  @PostMapping("/kudos/{uuid}")
  @ApiOperation("点赞")
  @KudosNumber(description = "动态点赞", key = KudosNumberKeyEnum.DYNAMIC_KUDOS, isKudos = true)
  @Limit(key = "DYNAMIC_KUDOS", permitsPerSecond = 2, timeout = 1000, msg = "系统繁忙，请稍后再试！")
  public ApiResponse<Void> kudos(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.kudos(uuid, currentLoginUuid());
    return success();
  }

  @PostMapping("/cancel-kudos/{uuid}")
  @ApiOperation("取消点赞")
  @KudosNumber(description = "动态取消点赞", key = KudosNumberKeyEnum.DYNAMIC_KUDOS, isKudos = false)
  @Limit(key = "DYNAMIC_KUDOS", permitsPerSecond = 2, timeout = 1000, msg = "系统繁忙，请稍后再试！")
  public ApiResponse<Void> cancelKudos(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    dynamicService.cancelKudos(uuid, currentLoginUuid());
    return success();
  }

  @GetMapping("/is-kudos/{uuid}")
  @ApiOperation("动态是否点赞")
  public ApiResponse<Boolean> isKudos(
      @PathVariable(value = "uuid", required = true) @ApiParam(value = "动态uuid", required = true) String uuid) {
    return success(dynamicService.isKudos(uuid, currentLoginUuid()));
  }
}
